package com.haima.sage.bigdata.api.frame.datatype.impl;

import java.util.regex.Pattern;

import com.haima.sage.bigdata.api.frame.datatype.DataTypeService;

public class ByteDataTypeService extends DataTypeService<Byte> {
    private Pattern pattern = Pattern.compile("\\d+");

    @Override
    public Byte trans(Object value) {
        if (null == value) {
            return null;
        }
        if (value instanceof Number) {
            return ((Number) value).byteValue();
        }

        String dist = value.toString().trim();
        // TODO config for pattern, parse which faster
        if (pattern.matcher(dist).matches()) {
            return Byte.parseByte(value.toString());
        }
        return null;
    }

}
